package com.qiangesoft.blog.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 统一结果返回
 *
 * @author qiange
 */
@ApiModel(value = "统一返回结果类")
@Data
public class ResponseResult<T> {
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "响应消息", required = false)
    private String message;

    /**
     * 响应码：参考`ResultCode`
     */
    @ApiModelProperty(value = "响应码", required = true)
    private Integer code;

    /**
     * 响应中的数据
     */
    @ApiModelProperty(value = "响应数据", required = false)
    private T data;

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public static ResponseResult error(String message) {
        return new ResponseResult(ResultCode.FAILURE.getCode(), message, null);
    }

    public static ResponseResult error() {
        return new ResponseResult(ResultCode.FAILURE.getCode(), ResultCode.ERROR.getDesc(), null);
    }

    public static ResponseResult error(Integer code, String message) {
        return new ResponseResult(code, message, null);
    }

    public static ResponseResult success() {
        return new ResponseResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), null);
    }

    public static <T> ResponseResult success(T data) {
        return new ResponseResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), data);
    }

    public static <T> ResponseResult success(String message, T data) {
        return new ResponseResult(ResultCode.SUCCESS.getCode(), message, data);
    }

    public static <T> ResponseResult success(Integer code, String message, T data) {
        return new ResponseResult(code, message, data);
    }

    public static ResponseResult success(Integer code, String message) {
        return new ResponseResult(code, message, null);
    }
}
